<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- uartassistant.qdoc -->
  <title>QtExtSerialPort : UartAssistant Demo</title>
  <link rel="stylesheet" type="text/css" href="style/style.css" />
</head>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td class="postheader" valign="center"><a href="index.html">Home</a> &middot; <a href="classes.html">All Classes</a> &middot;</td></tr></table><li><a href="http://doc.qt.nokia.com/qt5/all-examples.html">Examples</a></li>
<li>UartAssistant Demo</li>
<h1 class="title">UartAssistant Demo</h1>
<span class="subtitle"></span>
<!-- $$$examples/uartassistant-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="examples-uartassistant-dialog-cpp.html">examples/uartassistant/dialog.cpp</a></li>
<li><a href="examples-uartassistant-dialog-h.html">examples/uartassistant/dialog.h</a></li>
<li><a href="examples-uartassistant-dialog-ui.html">examples/uartassistant/dialog.ui</a></li>
<li><a href="examples-uartassistant-hled-cpp.html">examples/uartassistant/hled.cpp</a></li>
<li><a href="examples-uartassistant-hled-h.html">examples/uartassistant/hled.h</a></li>
<li><a href="examples-uartassistant-main-cpp.html">examples/uartassistant/main.cpp</a></li>
<li><a href="examples-uartassistant-uartassistant-pro.html">examples/uartassistant/uartassistant.pro</a></li>
<li><a href="examples-uartassistant-uartassistant-compat-pro.html">examples/uartassistant/uartassistant_compat.pro</a></li>
</ul>
<p>The example demonstrates how to use <a href="qextserialport.html">QextSerialPort</a>.</p>
<p>Initialze UI element.</p>
<pre class="cpp">    <span class="preprocessor">#ifdef Q_OS_WIN</span>
    ui<span class="operator">-</span><span class="operator">&gt;</span>portBox<span class="operator">-</span><span class="operator">&gt;</span>addItems(<span class="type"><a href="http://doc.qt.nokia.com/qt5/qstringlist.html">QStringList</a></span>()<span class="operator">&lt;</span><span class="operator">&lt;</span><span class="string">&quot;COM1&quot;</span><span class="operator">&lt;</span><span class="operator">&lt;</span><span class="string">&quot;COM2&quot;</span><span class="operator">&lt;</span><span class="operator">&lt;</span><span class="string">&quot;COM3&quot;</span><span class="operator">&lt;</span><span class="operator">&lt;</span><span class="string">&quot;COM4&quot;</span>);
    <span class="preprocessor">#else</span>
    ui<span class="operator">-</span><span class="operator">&gt;</span>portBox<span class="operator">-</span><span class="operator">&gt;</span>addItems(<span class="type"><a href="http://doc.qt.nokia.com/qt5/qstringlist.html">QStringList</a></span>()<span class="operator">&lt;</span><span class="operator">&lt;</span><span class="string">&quot;/dev/ttyS0&quot;</span><span class="operator">&lt;</span><span class="operator">&lt;</span><span class="string">&quot;/dev/ttyS1&quot;</span><span class="operator">&lt;</span><span class="operator">&lt;</span><span class="string">&quot;/dev/ttyUSB0&quot;</span><span class="operator">&lt;</span><span class="operator">&lt;</span><span class="string">&quot;/dev/ttyUSB1&quot;</span>);
    <span class="preprocessor">#endif</span>
    <span class="comment">//make sure user can input their own port name!</span>
    ui<span class="operator">-</span><span class="operator">&gt;</span>portBox<span class="operator">-</span><span class="operator">&gt;</span>setEditable(<span class="keyword">true</span>);

    ui<span class="operator">-</span><span class="operator">&gt;</span>baudRateBox<span class="operator">-</span><span class="operator">&gt;</span>addItems(QextSerialHelper<span class="operator">::</span>getBaudRateList());
    ui<span class="operator">-</span><span class="operator">&gt;</span>baudRateBox<span class="operator">-</span><span class="operator">&gt;</span>setCurrentIndex(<span class="number">3</span>);

    ui<span class="operator">-</span><span class="operator">&gt;</span>parityBox<span class="operator">-</span><span class="operator">&gt;</span>addItems(QextSerialHelper<span class="operator">::</span>getParityList());

    ui<span class="operator">-</span><span class="operator">&gt;</span>dataBitsBox<span class="operator">-</span><span class="operator">&gt;</span>addItems(QextSerialHelper<span class="operator">::</span>getDataBitsList());
    ui<span class="operator">-</span><span class="operator">&gt;</span>dataBitsBox<span class="operator">-</span><span class="operator">&gt;</span>setCurrentIndex(<span class="number">3</span>);

    ui<span class="operator">-</span><span class="operator">&gt;</span>stopBitsBox<span class="operator">-</span><span class="operator">&gt;</span>addItems(QextSerialHelper<span class="operator">::</span>getStopBitsList());</pre>
<p>Initialize serial port</p>
<pre class="cpp">    port <span class="operator">=</span> <span class="keyword">new</span> QextSerialPort(ui<span class="operator">-</span><span class="operator">&gt;</span>portBox<span class="operator">-</span><span class="operator">&gt;</span>currentText());
    port<span class="operator">-</span><span class="operator">&gt;</span>setBaudRate(QextSerialPort<span class="operator">::</span>BAUD9600);
    port<span class="operator">-</span><span class="operator">&gt;</span>setDataBits(QextSerialPort<span class="operator">::</span>DATA_8);
    port<span class="operator">-</span><span class="operator">&gt;</span>setParity(QextSerialPort<span class="operator">::</span>PAR_NONE);
    port<span class="operator">-</span><span class="operator">&gt;</span>setStopBits(QextSerialPort<span class="operator">::</span>STOP_1);
    port<span class="operator">-</span><span class="operator">&gt;</span>setFlowControl(QextSerialPort<span class="operator">::</span>FLOW_OFF);</pre>
<p>port Settings</p>
<pre class="cpp">    <span class="type">void</span> Dialog<span class="operator">::</span>onPortSettingsChanged()
    {
        port<span class="operator">-</span><span class="operator">&gt;</span>setBaudRate(QextSerialHelper<span class="operator">::</span>toBaudRateType(ui<span class="operator">-</span><span class="operator">&gt;</span>baudRateBox<span class="operator">-</span><span class="operator">&gt;</span>currentText()));
        port<span class="operator">-</span><span class="operator">&gt;</span>setParity(QextSerialHelper<span class="operator">::</span>toParityType(ui<span class="operator">-</span><span class="operator">&gt;</span>parityBox<span class="operator">-</span><span class="operator">&gt;</span>currentText()));
        port<span class="operator">-</span><span class="operator">&gt;</span>setDataBits(QextSerialHelper<span class="operator">::</span>toDataBitsType(ui<span class="operator">-</span><span class="operator">&gt;</span>dataBitsBox<span class="operator">-</span><span class="operator">&gt;</span>currentText()));
        port<span class="operator">-</span><span class="operator">&gt;</span>setStopBits(QextSerialHelper<span class="operator">::</span>toStopBitsType(ui<span class="operator">-</span><span class="operator">&gt;</span>stopBitsBox<span class="operator">-</span><span class="operator">&gt;</span>currentText()));
        port<span class="operator">-</span><span class="operator">&gt;</span>setTimeout(ui<span class="operator">-</span><span class="operator">&gt;</span>timeoutBox<span class="operator">-</span><span class="operator">&gt;</span>value());
    }</pre>
<p>Open or Close the port.</p>
<pre class="cpp">    <span class="type">void</span> Dialog<span class="operator">::</span>onOpenCloseButtonClicked()
    {
        <span class="keyword">if</span> (<span class="operator">!</span>port<span class="operator">-</span><span class="operator">&gt;</span>isOpen()) {
            port<span class="operator">-</span><span class="operator">&gt;</span>setPortName(ui<span class="operator">-</span><span class="operator">&gt;</span>portBox<span class="operator">-</span><span class="operator">&gt;</span>currentText());
            port<span class="operator">-</span><span class="operator">&gt;</span>open(<span class="type"><a href="http://doc.qt.nokia.com/qt5/qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadWrite);
        }
        <span class="keyword">else</span> {
            port<span class="operator">-</span><span class="operator">&gt;</span>close();
        }

        ui<span class="operator">-</span><span class="operator">&gt;</span>led<span class="operator">-</span><span class="operator">&gt;</span>turnOn(port<span class="operator">-</span><span class="operator">&gt;</span>isOpen());
    }</pre>
<p>Read from or Write to the port</p>
<pre class="cpp">    <span class="type">void</span> Dialog<span class="operator">::</span>onSendButtonClicked()
    {
        <span class="keyword">if</span> (port<span class="operator">-</span><span class="operator">&gt;</span>isOpen() <span class="operator">&amp;</span><span class="operator">&amp;</span> <span class="operator">!</span>ui<span class="operator">-</span><span class="operator">&gt;</span>sendEdit<span class="operator">-</span><span class="operator">&gt;</span>toPlainText()<span class="operator">.</span>isEmpty())
            port<span class="operator">-</span><span class="operator">&gt;</span>write(ui<span class="operator">-</span><span class="operator">&gt;</span>sendEdit<span class="operator">-</span><span class="operator">&gt;</span>toPlainText()<span class="operator">.</span>toLatin1());
    }

    <span class="type">void</span> Dialog<span class="operator">::</span>onReadyRead()
    {
        <span class="keyword">if</span> (port<span class="operator">-</span><span class="operator">&gt;</span>bytesAvailable()) {
            ui<span class="operator">-</span><span class="operator">&gt;</span>recvEdit<span class="operator">-</span><span class="operator">&gt;</span>moveCursor(<span class="type"><a href="http://doc.qt.nokia.com/qt5/qtextcursor.html">QTextCursor</a></span><span class="operator">::</span>End);
            ui<span class="operator">-</span><span class="operator">&gt;</span>recvEdit<span class="operator">-</span><span class="operator">&gt;</span>insertPlainText(<span class="type"><a href="http://doc.qt.nokia.com/qt5/qstring.html">QString</a></span><span class="operator">::</span>fromLatin1(port<span class="operator">-</span><span class="operator">&gt;</span>readAll()));
        }
    }</pre>
<p class="centerAlign"><img src="images/uartassistant.png" alt="" /></p></div>
<!-- @@@examples/uartassistant -->
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2000-2012</td>
<td width="20%" align="center"><a href="http://code.google.com/p/qextserialport">QtExtSerialPort Project</a></td>
<td width="40%" align="right"><div align="right">QextSerialPort Manual</div></td>
</tr></table></div></address></body>
</html>
